package com.example.warehouse.service;

import com.example.warehouse.dto.StockRequestDTO;
import com.example.warehouse.entity.Inventory;
import com.example.warehouse.entity.InventoryLog;
import com.example.warehouse.entity.Product;
import com.example.warehouse.enums.OperationType;
import com.example.warehouse.mapper.InventoryLogMapper;
import com.example.warehouse.mapper.InventoryMapper;
import com.example.warehouse.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class InventoryLogService {
    @Autowired
    private InventoryMapper  inventoryMapper;

    @Autowired
    private InventoryLogMapper inventoryLogMapper;

    @Autowired
    private ProductMapper productMapper;  // 添加 productMapper，查询商品信息

    //入库操作
    public void stockIn(Long productId,int quantity,String operator){
        Inventory inventory = inventoryMapper.findByProductId(productId);
        if(inventory == null){
            inventory = new Inventory();
            inventory.setProductId(productId);
            inventory.setQuantity(0);

        }
        //更新库存
        inventory.setQuantity(inventory.getQuantity() + quantity);
        inventoryMapper.updateInventory(inventory);

        //记录流水
        InventoryLog log = new InventoryLog();
        log.setProductId(productId);
        log.setQuantity(quantity);
        log.setOperator(operator);
        log.setOperationType(OperationType.STOCK_IN);
      inventoryLogMapper.insertInventoryLog(log);
    }

    //出库操作
    public void stockOut(Long productId,int quantity,String operator){
        Inventory inventory = inventoryMapper.findByProductId(productId);
        if(inventory == null || inventory.getQuantity() < quantity){
            throw new RuntimeException("库存不足");

        }

        //更新库存
        inventory.setQuantity(inventory.getQuantity() - quantity);
        inventoryMapper.updateInventory(inventory);


        //记录流水
        InventoryLog log = new InventoryLog();
        log.setProductId(productId);
        log.setQuantity(quantity);
        log.setOperator(operator);
        log.setOperationType(OperationType.STOCK_OUT);
        log.setOperatedAt(LocalDateTime.now());
        inventoryLogMapper.insertInventoryLog(log);
    }

    // 调整库存
    public void adjustStock(Long productId, int quantity, String operator) {
        // 1. 查询商品信息 && 查询当前商品的库存
        Product product = productMapper.findById(productId); // 根据 productId 查询商品信息
        Integer currentQuantity = inventoryMapper.getInventoryByProductId(productId);
        if (product == null && currentQuantity == null) {
            throw new IllegalArgumentException("商品不存在或库存信息丢失");
        }

        // 2. 调整库存
        int newQuantity = currentQuantity + quantity; // quantity 为正数表示入库，负数表示出库
        inventoryMapper.updateInventory(new Inventory(productId, newQuantity)); // 更新库存


        // 3. 记录库存调整日志
        InventoryLog inventoryLog = new InventoryLog();
        inventoryLog.setProductId(productId);
        inventoryLog.setQuantity(quantity); // 记录调整数量
        inventoryLog.setOperator(operator); // 操作员
        inventoryLog.setOperationType(OperationType.STOCK_ADJUST); // 操作类型为库存调整
        inventoryLog.setOperatedAt(LocalDateTime.now()); // 当前时间作为操作时间
        inventoryLogMapper.insertInventoryLog(inventoryLog); // 插入日志
    }

    // 批量入库操作
    public void batchStockIn(List<StockRequestDTO> stockInRequests) {
        // 用于存储所有入库的日志
        List<InventoryLog> inventoryLogs = new ArrayList<>();

        for (StockRequestDTO request : stockInRequests) {
            // 获取当前商品的库存
            Integer currentStock = inventoryMapper.getInventoryByProductId(request.getProductId());
            if (currentStock == null) {
                // 如果当前库存为空，则初始化库存为 0
                currentStock = 0;
            }

            // 更新库存
            Integer newStock = currentStock + request.getQuantity();

            // 创建 Inventory 实体对象，设置 productId 和 newStock
            Inventory inventory = new Inventory();
            inventory.setProductId(request.getProductId());
            inventory.setQuantity(newStock); // 设置新的库存数量

            // 调用 updateInventory 方法传入 Inventory 实体
            inventoryMapper.updateInventory(inventory);

            // 记录入库日志
            InventoryLog inventoryLog = new InventoryLog();
            inventoryLog.setProductId(request.getProductId());
            inventoryLog.setOperationType(OperationType.STOCK_IN); // 操作类型为入库
            inventoryLog.setQuantity(request.getQuantity());
            inventoryLog.setOperatedAt(LocalDateTime.now()); // 当前时间作为操作时间
            inventoryLog.setOperator("admin");
            // 将入库日志添加到列表中
            inventoryLogs.add(inventoryLog);
        }

        // 批量插入所有入库日志
        inventoryLogMapper.insertInventoryLogBatch(inventoryLogs);
    }

    // 批量出库操作
    public void batchStockOut(List<StockRequestDTO> stockOutRequests) {
        // 用于存储所有出库的日志
        List<InventoryLog> inventoryLogs = new ArrayList<>();

        for (StockRequestDTO request : stockOutRequests) {
            // 获取当前商品的库存
            Integer currentStock = inventoryMapper.getInventoryByProductId(request.getProductId());
            if (currentStock == null || currentStock < request.getQuantity()) {
                // 库存不足，不能出库
                throw new RuntimeException("商品 " + request.getProductId() + " 库存不足，无法出库");
            }

            // 更新库存
            Integer newStock = currentStock - request.getQuantity();

            // 创建 Inventory 实体对象，设置 productId 和 newStock
            Inventory inventory = new Inventory();
            inventory.setProductId(request.getProductId());
            inventory.setQuantity(newStock); // 设置新的库存数量

            // 调用 updateInventory 方法传入 Inventory 实体
            inventoryMapper.updateInventory(inventory);

            // 记录出库日志
            InventoryLog inventoryLog = new InventoryLog();
            inventoryLog.setProductId(request.getProductId());
            inventoryLog.setOperationType(OperationType.STOCK_OUT); // 出库操作
            inventoryLog.setQuantity(request.getQuantity());
            inventoryLog.setOperatedAt(LocalDateTime.now()); // 当前时间作为操作时间
            inventoryLog.setOperator("admin");
            // 将出库日志添加到列表中
            inventoryLogs.add(inventoryLog);
        }

        // 批量插入所有出库日志
        inventoryLogMapper.insertInventoryLogBatch(inventoryLogs);
    }

}
